<template>
    <div>
        <transition @before-enter="ballBefore" @enter="ballEnter" @after-enter="ballAfter">
            <div ref="ball" class="ball" v-show="ballFlag"></div>
        </transition>
        <div class="mui-card">
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <swipe :swipeList="swipeList" :isFull="false"></swipe>
                </div>
            </div>
        </div>
        <div class="mui-card">
            <div class="mui-card-header">{{data.title}}</div>
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <p class="price">
                        <span>市场价:<span class="marketPrice">￥{{data.market_price}}</span> </span> &nbsp;&nbsp;&nbsp;
                        <span>销售价：<span class="cellPrcie">￥{{data.sell_price}}</span></span>
                    </p>
                    <div class="num"><span>购买数量：</span>
                        <numberBox @getnumber="getNumber" :max="data.stock_quantity"></numberBox>
                    </div>
                    <mt-button type="primary" size="small">立即购买</mt-button>
                    <mt-button type="danger" size="small" @click="addToCart(data.sell_price)">加入购物车</mt-button>
                </div>
            </div>
        </div>
        <div class="mui-card">
            <div class="mui-card-header">商品参数</div>
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <p>商品货号：{{data.goods_no}}</p>
                    <p>库存情况：{{data.stock_quantity}}</p>
                    <p>上架时间：{{data.add_time}}</p>
                </div>
            </div>
            <div class="mui-card-footer">
                <mt-button type="primary" size="large" plain @click="goInfo(data.id)">图文介绍</mt-button>
                <mt-button type="danger" size="large" plain @click="goComment(data.id)">商品评论</mt-button>
            </div>
        </div>
    </div>
</template>

<script>
    import swipe from '../subcomponents/swipe.vue';
    import {
        toast
    } from 'mint-ui';
    import numberBox from '../subcomponents/numberBox.vue';
    import {
        setTimeout
    } from 'timers';
    export default {
        data() {
            return {
                id: this.$route.params.id,
                swipeList: [],
                data: {},
                ballFlag: false,
                count: 1
            }
        },
        methods: {
            getSwipeList() {
                this.$http.get(`api/getthumimages/${this.id}`).then(res => {
                    if (res.body.status == 0) {
                        res.body.message.forEach(item => {
                            item.img = item.src;
                            this.swipeList.push(item);
                        });

                    } else {
                        toast('接受图片数据失败');
                    }
                });
            },
            getData() {
                this.$http.get(`api/goods/getinfo/${this.id}`).then(res => {
                    if (res.body.status == 0) {
                        this.data = res.body.message[0];
                    } else {
                        toast('获取数据失败');
                    }
                })
            },
            goInfo(id) {
                this.$router.push(`/index/goodsInfo/${id}`);
            },
            goComment(id) {
                this.$router.push(`/index/goodsComment/${id}`);
            },
            addToCart(price) {
                this.ballFlag = !this.ballFlag;
                let goods = {
                    id: this.id,
                    count: this.count,
                    price: price,
                    selected: false
                }
                this.$store.commit('addToCar', goods);
            },
            ballBefore(el) {
                el.style.transform = "translate(0,0)";
            },
            ballEnter(el, done) {
                el.offsetWidth;
                const rec = document.getElementById('badge').getBoundingClientRect();
                const recBall = this.$refs.ball.getBoundingClientRect();
                let x = rec.left - recBall.left;
                let y = rec.top - recBall.top;
                el.style.transform = `translate(${x}px,${y}px)`;
                el.style.transition = 'all 0.5s ease';
                setTimeout(() => {
                    this.ballFlag = !this.ballFlag;
                }, 200)
                // 此处有问题但不知道如何解决？
            },
            ballAfter(el) {
                // this.ballFlag = ! this.ballFlag;
            },
            getNumber(count) {
                this.count = count;
            }
        },
        components: {
            swipe: swipe,
            numberBox: numberBox
        },
        created() {
            this.getSwipeList();
            this.getData();
        }
    }
</script>

<style lang="less" scope>
    .mui-card-content-inner {
        p {
            font-size: 12px;
        }

        .price {
            .cellPrcie {
                font-size: 14px;
                color: red;
            }

            .marketPrice {
                text-decoration: line-through;
            }
        }

        .num {
            font-size: 14px;
            color: #8f8f94;
            margin-bottom: 5px;
        }
    }

    .mui-card-footer {
        display: block;

        button:first-child {
            margin-bottom: 10px;
        }
    }

    .ball {
        width: 15px;
        height: 15px;
        background-color: red;
        border-radius: 50%;
        position: absolute;
        z-index: 100;
        top: 407px;
        left: 150px;
    }
</style>